<?php
require('functions/page_load.php');
require("fckeditor/fckeditor.php");

$settings_nav = true;
$active_tab = 'details';

$results = ($_GET['new'] == 'true') ? '<span class="success_result" style="margin-bottom: 10px;">Trade successfully created.</span>' : $results ;

if(isset($_GET['delete_trade'])){

		$active_tab = 'trades';

		$sql = "DELETE FROM trades_to_trades WHERE id = '".mysql_real_escape_string($_GET['delete_trade'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Sub-trade deleted successfully!</div>';

}

if(isset($_GET['delete_trade_content'])){

		$active_tab = 'trade_content_container';

		$sql = "DELETE FROM trade_content WHERE id = '".mysql_real_escape_string($_GET['delete_trade_content'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Trade content deleted successfully!</div>';

}


if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if(isset($_POST['add_trade'])){
	
		$active_tab = 'trades';
	
		$sql = "INSERT INTO trades_to_trades SET trade_id = '".mysql_real_escape_string($_POST['trade_id'])."', parent_id = '".mysql_real_escape_string($_GET['id'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Sub-trade added successfully!</div>';
	
	}
	
	if(isset($_POST['add_trade_content'])){
	
		$active_tab = 'trade_content_container';
	
		$sql = "INSERT INTO trade_content SET trade_id = '".mysql_real_escape_string($_GET['id'])."', area_id = '".mysql_real_escape_string($_POST['area_id'])."', content = '".mysql_real_escape_string(stripslashes($_POST['trade_content_2']))."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Trade content added successfully!</div>';
	
	}

	if(isset($_POST['edit_details'])){

		$sql = "UPDATE trades SET 
				trade = '".mysql_real_escape_string($_POST['trade'])."',
				url = '".mysql_real_escape_string($_POST['url'])."',
				lead_cost = '".mysql_real_escape_string($_POST['lead_cost'])."',
				trade_content = '".mysql_real_escape_string(stripslashes($_POST['trade_content']))."'
				WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Trade updated successfully!</div>';
		
	}

}

$sql = "SELECT trade, url, lead_cost, trade_content FROM trades WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="banners.php">Banners</a></li>
<li><a href="trades.php">Trades</a></li>
<li><a href="directory_categories.php">Directory Categories</a></li>
<li><a href="subscriptions.php">Subscriptions</a></li>
<li><a href="sms_packages.php">SMS Packages</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Edit Trade</h2>
	
	<div id="top_links">
	<a class="tab_link" rel="#trade_content_container" style="background-image: url(images/list.png); padding-left: 20px;">Trade Content</a>
	<a class="tab_link" rel="#trades" style="background-image: url(images/list.png); padding-left: 20px;">Sub-trades</a>
	<a class="tab_link" rel="#details" style="background-image: url(images/page.png); padding-left: 20px;">Trade details</a>
	<a href="trades.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Trades</a>
	</div>
	
	<?php echo $results; ?>
	
	<div id="details" class="tab_container"<?php echo ($active_tab == 'details') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
	
			<form method="post" action="">
			
			<input type="hidden" name="edit_details" value="true" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="name">Trade*:</label></div>
			<div class="form_row_right"><input type="text" name="trade" id="name" value="<?php echo $rs['trade']; ?>" onkeyup="buildUrl();" onkeydown="buildUrl();" /></div>
			</div>

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="url">Trade URL*:</label></div>
			<div class="form_row_right"><input type="text" name="url" id="url" value="<?php echo $rs['url']; ?>" /> <span>This must be a unique and web-safe name for the trade.</span></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="lead_cost">Lead cost:</label></div>
			<div class="form_row_right"><input type="text" name="lead_cost" id="lead_cost" value="<?php echo $rs['lead_cost']; ?>" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label>Content:</label></div>
			<div class="form_row_right">
			<?php
			$oFCKeditor = new FCKeditor('trade_content');
			$oFCKeditor->Height = '400';
			$oFCKeditor->BasePath = '/admin/fckeditor/';
			$oFCKeditor->Value = $rs['trade_content'];
			$oFCKeditor->Create();
			?>
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
	</div>
			
	<div id="trades" class="tab_container"<?php echo ($active_tab == 'trades') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
			
	
	<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Trade</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT trades_to_trades.id, trade FROM trades_to_trades 
				LEFT JOIN trades ON trades_to_trades.trade_id = trades.id 
				WHERE parent_id = ".mysql_real_escape_string($_GET['id'])."";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			echo '<tr><td align="left" valign="top">'.$rs['trade'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_trade='.$rs['id'].'\',\'this sub-trade from this trade\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="2">There are currently no sub-trades for this trade.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
			<form method="post" action="">
			
			<input type="hidden" name="add_trade" value="true" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Add trade:</label></div>
			<div class="form_row_right"><select name="trade_id" id="trade_id">
			<?php
			
			$sql = "SELECT id, trade FROM trades ORDER BY trade";
			$trades_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trades_rs = mysql_fetch_assoc($trades_query)){
			
				echo '<option value="'.$trades_rs['id'].'">'.$trades_rs['trade'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
			
			
	</div>
	
	<div id="trade_content_container" class="tab_container"<?php echo ($active_tab == 'trade_content_container') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
			
	
	<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Area</th><th align="left">Content</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT trade_content.id, area, content FROM trade_content 
				LEFT JOIN areas ON trade_content.area_id = areas.id 
				WHERE trade_id = ".mysql_real_escape_string($_GET['id'])."";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			echo '<tr><td align="left" valign="top">'.$rs['area'].'</td><td align="left" valign="top" style="font-size: 16px;">'.$rs['content'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_trade_content='.$rs['id'].'\',\'this content from this trade\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="2">There are currently no content records for this trade.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
			<p><strong>Add content</strong></p>
		
			<form method="post" action="">
			
			<input type="hidden" name="add_trade_content" value="true" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Area:</label></div>
			<div class="form_row_right"><select name="area_id" id="area_id">
			<?php
			
			$sql = "SELECT id, area FROM areas ORDER BY area";
			$trades_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trades_rs = mysql_fetch_assoc($trades_query)){
			
				echo '<option value="'.$trades_rs['id'].'">'.$trades_rs['area'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label>Content:</label></div>
			<div class="form_row_right">
			<?php
			$oFCKeditor = new FCKeditor('trade_content_2');
			$oFCKeditor->Height = '400';
			$oFCKeditor->BasePath = '/admin/fckeditor/';
			$oFCKeditor->Value = '';
			$oFCKeditor->Create();
			?>
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
			
			
	</div>
			

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>